<?php

namespace App\Admin\Controllers;

use App\Admin\Tools;
use App\Models\Attribute;
use App\Models\Category;
use App\Models\Coupon;
use App\Models\Image;
use App\Models\Product;
use Encore\Admin\Controllers\AdminController as Controller;
use App\Models\Tag;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ProductController extends Controller
{
    protected $title = "商品管理";

    protected function grid()
    {
        $grid = new Grid(new Product);

        $grid->filter(function($filter){
            $filter->like('name', '商品名');
            $filter->like('category.id','分类')->select(Category::query()->pluck('name','id')->toArray());
        });
        $grid->model()->where("status", ">=", 0);

        $grid->id('商品ID')->sortable();
        $grid->name('商品名');
        $grid->category()->name('分类');
        $grid->count_reviews('浏览量')->sortable();
        $grid->count_sales('销售量')->sortable();
        $grid->count_comments('评论数')->sortable();
        $grid->price('价格')->display(function ($prices) {
            return Tools::price($prices);
        });
        $grid->status('是否上架')->switch([
            'on' => ['value' => 1, 'text' => '上架', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
        ]);
        $grid->updated_at('更新时间')->sortable();

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Product::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->summary('Summary');
        $show->category_id('Category id');
        $show->status('Status');
        $show->count_reviews('Count reviews');
        $show->count_sales('Count sales');
        $show->count_comments('Count comments');
        $show->price('Price');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    protected function form()
    {
        $form = new Form(new Product);

        $form->tab('基本', function (Form $form) {
            $form->text('name', '商品名称');
            $form->select('category_id', '商品分类')
                ->options(Category::options());
            $form->image('cover.path', '封面');
            $form->hidden('cover.type')->value(Image::PRODUCT_COVER);
            $form->text('current', '现价');
            $form->text('original', '原价');
            $form->text('wholesale', '批发价格');
            $form->switch('status', '是否上架')->states([
                'on' => ['value' => 1, 'text' => '上架', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
            ]);
            $form->multipleSelect('tags', '商品标签')
                ->options(Tag::all()->pluck('name', 'id'));
            $form->multipleSelect('coupons', '优惠活动')
                ->options(Coupon::all()->pluck('name', 'id'));
            $form->simditor('summary', '商品详情');
        })->tab('SKU属性', function (Form $form) {
            $form->hasMany('skus', 'SKU属性', function (Form\NestedForm $form) {
                $form->select('attribute_id', '商品属性')
                    ->options(Attribute::all()->pluck('name', 'id'));
                $form->text('attribute_value');
            });
        })->tab("图集", function (Form $form) {
            $form->hasMany('images', '图集', function (Form\NestedForm $form) {
                $form->image('path', "图集");
                $form->hidden('type')->value(Image::PRODUCT_IMAGES);
            });
        });

        return $form;
    }
}
